<?php

/**
 * This is the model class for table "sale_orders".
 *
 * The followings are the available columns in table 'sale_orders':
 * @property integer $id
 * @property integer $organization_id
 * @property integer $customer_id
 * @property integer $so_number
 * @property integer $order_date
 * @property integer $status_id
 */
class SalesOrder extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return SalesOrder the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sale_orders';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('organization_id, customer_id, so_number, order_date, status_id', 'required'),
			array('organization_id, customer_id, status_id', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, organization_id, customer_id, so_number, order_date, status_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'customer' => array(self::BELONGS_TO, 'Customer', 'customer_id'),
			'status' => array(self::BELONGS_TO, 'Status', 'status_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'organization_id' => 'Organization',
			'customer_id' => 'Customer',
			'so_number' => 'So Number',
			'order_date' => 'Order Date',
			'status_id' => 'Status',
		);
	}
	
	public function getTotalAmount()
	{
		$line_items_array = SalesOrderLineItem::findLineItems($this->id);
		
		$total_amount = 0;
		
		foreach($line_items_array as $line_item)
		{
			$total_amount += $line_item->item->getCost();
		}
		
		return $total_amount;
	}
	
	public function behaviors()
	{ 
		return array( 'LoggableBehavior'=> 'application.modules.auditTrail.behaviors.LoggableBehavior', ); 
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('organization_id',$this->organization_id);
		$criteria->compare('customer_id',$this->customer_id);
		$criteria->compare('so_number',$this->so_number);
		$criteria->compare('order_date',$this->order_date);
		$criteria->compare('status_id',$this->status_id);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}